package top.yeyusmile.service;

import com.google.gson.Gson;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yeyusmile.common.MyTemplate;
//import top.yeyusmile.common.TypeConstant;
import top.yeyusmile.mirai.Sender;
import top.yeyusmile.mirai.msg.PlainMsg;
import top.yeyusmile.pojo.QQResult;
import top.yeyusmile.utils.HttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 夜雨
 * @Web www.yeyusmile.top
 * @date 2022/3/16 19:18
 */
@Service
public class QbangServiceImpl implements QbangService {


    @Autowired
    private MyTemplate myTemplate;

    /**
     * 解码
     *
     * @param ciphertext
     * @return
     */
    private String decode(int[] ciphertext) {

        char decode[] = new char[ciphertext.length];

        for (int i = 0; i < ciphertext.length; i++) {
            decode[i] = (char) (ciphertext[i] - 5);
        }
        return new String(decode);
    }


    @Override
    public void query(String keyword, Sender sender, int queryType) {

        String host1 = decode(new int[]{109, 121, 121, 117, 120, 63, 52, 52, 127, 126, 51,
                125, 126, 124, 113, 102, 117, 110, 51, 104, 104});


        final String api1url = host1 + "/qqcx?qq=" + keyword;
        final String api2url = host1 + "/qqphone?phone=" + keyword;
        final String api3url = host1 + "/lolname?name=" + keyword;
        final String api4url = host1 + "/wbapi?id=" + keyword;
        final String api5url = host1 + "/wbphone?phone=" + keyword;


        // doQuery(api1url, message, keyword);//综合

        if (queryType == 0) {
            doQuery(api1url, sender, keyword);
        }
        if (queryType == 1) {
            doQuery(api2url, sender, keyword);
        }
        if (queryType == 2) {
            doQuery(api3url, sender, keyword);
        }
        if (queryType == 3) {
            doQuery(api4url, sender, keyword);
        }
        if (queryType == 4) {
            doQuery(api5url, sender, keyword);
        }

    }

    /**
     * 查询
     *
     * @param url
     * @param sender
     * @param keyword 查询的内容
     */
    private void doQuery(String url, Sender sender, String keyword) {
        try {
            HttpUtil.httpGet(url,"", new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    List<Object> params = new ArrayList<>();
                    params.add(new PlainMsg("查询失败"));
                    myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());

                }

                @Override
                public void onResponse(Call call, final Response response) throws IOException {
                    Gson gson = new Gson();
                    QQResult qqResult = gson.fromJson(response.body().string(), QQResult.class);
                    qqResult.setQueryType("[" + keyword + "]");
                    List<Object> params = new ArrayList<>();
                    params.add(new PlainMsg("查询成功:\n" + qqResult + "\nPower by Mirai机器人."));
                    myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());
                }
            });

        } catch (Exception e) {
            List<Object> params = new ArrayList<>();
            params.add(new PlainMsg("查询失败"));
            myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());
            e.printStackTrace();
        }
    }
}
